using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Character.Jarily {
    [RequireComponent(typeof(AiBehaviourPool))]
    [RequireComponent(typeof(Animator))]
    [RequireComponent(typeof(Collider2D))]
    public class AImind : MonoBehaviour {
        /// <summary>
        /// AI意识 视野 视野指示器 攻击模块\近程攻击,远程攻击,投掷 动作模块
        /// 近战攻击 根据触发器范围及伤害选择攻击方式 具有冲击力 反冲 
        /// 远程攻击 根据可用远程攻击选择 
        /// 弹丸重力 弹丸类型\射线，动能弹丸 弹丸重力，弹丸速度，发射位置，
        /// AI行为池 近程攻击池 远程攻击池 投掷属性
        /// </summary>

        [SerializeField]
        bool right = false;
        /// <summary>
        /// 好吧AI还是先从简单的来吧。
        /// </summary>

        //Property m_property = Property.Fly;

        AiBehaviourPool behaviourpool = null;
        //Action<GameObject> eventFindEnemy = null;
        Move m_move = null;
        AIBehaviourAttribute m_attribute = AIBehaviourAttribute.LossAim;

        int Player = 0;

        [SerializeField]
        Eye eye = null;
        /// <summary>
        /// AI集团化 眼睛 事件
        /// 集团化，让数个游戏对象成为一个整体，通过Leader共享战斗信息
        /// 
        /// </summary>
        /// <returns></returns>

        private void Awake() {
            behaviourpool = GetComponent<AiBehaviourPool>();
            m_move = GetComponent<Move>();
            Player = LayerMask.NameToLayer("Player");
            real_right = right;
            layer_Obstacle = LayerMask.GetMask("Ground");
        }

        private void FixedUpdate() {
            Action();
        }


        void Action() {
            switch (m_attribute) {
            case AIBehaviourAttribute.partol:
                switch (FindEnemy()) {
                case "Enemy":
                    m_attribute = AIBehaviourAttribute.PursueAttack;
                    PursueAttack();
                    break;
                case "NO":
                    Partol();
                    break;
                case "Miss":
                    Miss();
                    break;
                }
                break;
            case AIBehaviourAttribute.PursueAttack:
                PursueAttack();
                break;
            }
        }

        GameObject aim;
        bool real_right;
        string FindEnemy() {
            //寻敌
            if (!aim) {
                RaycastHit2D[] ALLpoint = ResuableCode.SeeCast(eye.SeeDirectory, eye.SeeAngle, eye.SeeDistance, eye.SeeDensity, eye.SeeMask, eye.position, real_right);
                foreach (RaycastHit2D point in ALLpoint) {
                    if (point.collider.gameObject.layer == Player) {
                        aim = point.collider.gameObject;
                        return "Enemy";
                    }
                }
                return "NO";
            } else {
                RaycastHit2D point = Physics2D.Raycast(eye.position.position, aim.transform.position - eye.position.position);
                if (point.collider.gameObject == aim) {
                    return "Enemy";
                } else return "Miss";//Miss
            }
        }

        void Miss() {
            ////...
            //Debug.Log("Miss");
            aim = null;
            m_attribute = AIBehaviourAttribute.partol;
        }

        void PursueAttack() {
            RaycastHit2D point;
            try {
                Vector2 direction = aim.transform.position - eye.position.position;
                point = Physics2D.Raycast(eye.position.position, direction, eye.SeeDistance, eye.SeeMask);
            } catch {
                m_attribute = AIBehaviourAttribute.partol;
                return;
            }
            if (point.collider.gameObject.layer == Player) {
                Debug.Log("Pursue!");
                MoveTo();
            } else {
                Miss();
            }
        }

        bool Attack() {
            //...
            return false;
        }
        void MoveTo() {
            float x = aim.transform.position.x - transform.position.x;
            bool right = x > 0 ? true : false;
            ChangeDirection(right);
            //Debug.Log("Move");
            m_move.SetMove(right);
            m_move.Walk = true;
        }

        void Walk() {
            //...
        }
        int layer_Obstacle;
        float HaveObstacle() {
            RaycastHit2D front_point = Physics2D.Raycast(transform.position, right ? Vector2.right : Vector2.left, 2.0f, layer_Obstacle);
            if (front_point) {
                return 1;
            } else return 0;
        }
        /// <summary>
        /// 转换方向
        /// </summary>
        void ChangeDirection(bool right) {
            real_right = right;
            transform.rotation = Quaternion.Euler(0, this.right ^ right ? 180 : 0, 0);

        }
        void Partol() {
            Debug.Log("Partol!");
        }
        /// <summary>
        /// 实例化武器
        /// </summary>

        //transform m_instantiateobjecttrans;
        //void instanceobject(gameobject gameobject)
        //{
        //    gameobject.getcomponent<physicalprojectile>().setright(right);
        //    instantiate(gameobject, m_instantiateobjecttrans.position,
        //        right ? quaternion.euler(0, 0, gameobject.getcomponent<physicalprojectile>().rotation) : quaternion.euler(0, 180, gameobject.getcomponent<physicalprojectile>().rotation));
        //}
        //void setup()
        //{

        //}


#if UNITY_EDITOR
        private void OnDrawGizmosSelected() {
            Vector3 forward = right ? Quaternion.Euler(0, 0, eye.SeeDirectory - eye.SeeAngle * 0.5f) * Vector2.right : Quaternion.Euler(0, 180, eye.SeeDirectory + eye.SeeAngle * 0.5f) * Vector2.right;
            Handles.color = new Color(0, 1, 0, 0.2f);
            Handles.DrawSolidArc(eye.position.position, Vector3.forward, forward, eye.SeeAngle, eye.SeeDistance);
        }
#endif
    }
}